namespace my_pri_queue 
{
    template<class T>
    struct less 
    {
        bool operator()(const T& l, const T& r)
        {
            return l < r;
        }
    };

    template<class T>
    struct greater 
    {
        bool operator()(const T& l, const T& r)
        {
            return l > r;
        }
    };

    template<class T, class Container = vector<T>, class Compare = less<T>>
    class priority_queue 
    {
    public:
        priority_queue() {}

        template<class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                _con.push_back(*first);
                ++ first;
            }

            for (int i = (_con.size() - 2); i >= 0; i -- )
                adjustdown(i);
        }

        priority_queue(const priority_queue& pq) { _con = pq._con; }

        priority_queue& operator=(const priority_queue& pq)
        {
            _con = pq._con;
            return *this;
        }

        void adjustdown(size_t parent)
        {
            Compare Com;
            
            size_t child = parent * 2 + 1;
            while (child < size())
            {
                if (child + 1 < size() && Com(_con[child], _con[child + 1]))
                    child ++ ;
                if (Com(_con[parent], _con[child]))
                {
                    swap(_con[parent], _con[child]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else break;
            }
        }

        void adjustup(size_t child)
        {
            Compare Com;

            size_t parent = (child - 1) / 2;
            while (child > 0)
            {
                if (Com(_con[parent], _con[child]))
                {
                    swap(_con[parent], _con[child]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else break;
            }   
        }

        const T& top() const { return _con[0]; }

        void push(const T& x)
        {
            _con.push_back(x);
            adjustup(_con.size() - 1);
        }

        void pop()
        {
            swap(_con[0], _con[_con.size() - 1]);
            _con.pop_back();
            adjustdown(0);
        }

        bool empty() const { return _con.empty(); }

        size_t size() { return _con.size(); }

    private:
        Container _con;
    };
}
